<html>
<head>
<script language="javascript" type="text/javascript">

localStorage["txtDefault"] = "Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";;


function init() {
  if (localStorage["txtToBeUsed"] == null) localStorage["txtToBeUsed"] = localStorage["txtDefault"];
  if (localStorage["debug"] == null) localStorage["debug"] = false;
  canvas = document.getElementById('canvas');
  loggedInImage = document.getElementById('imgBlingText');
  canvasContext = canvas.getContext('2d');
  chrome.browserAction.setIcon({path: "bt_small.png"});
}


chrome.extension.onRequest.addListener(
  function(request, sender, sendResponse) {
		console.log(request.tagName);
                setBadgeText(request.tagName.toLowerCase());
		sendResponse({done: "1"});
  });


// Called when the user clicks - browser action.
chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.getSelected(null, function(tab) {
	  chrome.tabs.sendRequest(tab.id, {txt: localStorage["txtToBeUsed"]}, function(response) {
		console.log(response.farewell);
	  });
	});
});

var refAnimator = null;
function setBadgeText(strTxt)
{
    if (refAnimator) refAnimator.stop();
    if (strTxt.length > 4)
        {
            chrome.browserAction.setBadgeText({text:""});
            var animator = new BadgeTextAnimator ( {
                text: strTxt, // text to be scrolled (or animated)
                interval: 300, // the "speed" of the scrolling
                repeat: true, // repeat the animation or not
                countrepeat: 5, // how many time to repeat the loop; 0 = endless repeat
                size: 4 // size of the badge
            } );
            animator.animate ();
            refAnimator = animator;
        } else {
            chrome.browserAction.setBadgeText({text:strTxt});
        }

}

//chrome badge animator
//code developed by Jan HanÄ�iÄ�
//taken from:
//  http://hancic.info/badge-animator-for-chrome-extensions
function BadgeTextAnimator ( options ) {
	if ( options === undefined ) {
		return null;
	}

	this.options = {
		text: options.text,
		interval: ( options.interval === undefined ? 500 : options.interval ),
		repeat: ( options.repeat === undefined ? true : options.repeat ),
                countrepeat: (options.countrepeat === undefined ? 0 : options.countrepeat),
		size: ( options.size !== undefined && options.size > 0 && options.size <= 6 ? options.size : 6 )
	};

	this.intervalId = null;
	this.index = 0;
        this.repeatCounter = 1;
}

BadgeTextAnimator.prototype.doAnimate = function () {
	var startAt = this.index;
	var cutAt = 1; //this.options.size;
	var addBefore = false;
	if ( this.index < this.options.size ) {
		cutAt = this.index + 1;
		addBefore = true;
		startAt = 0;
	} else {
            cutAt = this.index + 1;
            addBefore = true;
            startAt =  this.index - this.options.size;

        }


	var chunk = this.options.text.substr ( startAt, cutAt );

	if ( chunk.length < this.options.size ) {
		var difference = this.options.size - chunk.length;
		for ( var i = 0; i <= difference; i++ ) {
			if ( addBefore === true ) {
				chunk = ' ' + chunk;
			} else {
				chunk = chunk + ' ';
			}
		}
	}

	chrome.browserAction.setBadgeText ( { text: chunk } );

	this.index = this.index + 1;
	if ( this.index === this.options.text.length ) {
		if (( this.options.repeat === true ) && ((this.repeatCounter < this.options.countrepeat))) {
                        this.index = 0;
                        if (this.options.countrepeat !== 0) this.repeatCounter++;
		} else {
			this.stop ();
		}
	}
};

BadgeTextAnimator.prototype.animate = function () {
	var spaces = Array ( '', ' ', '  ', '   ', '    ', '     ', '      ' );
	chrome.browserAction.setBadgeText ( { text: spaces[this.options.size] } );

	this.doAnimate ();
	this.intervalId = setInterval ( (function ( self ) {
			return function () {
				self.doAnimate();
			};
		} ) ( this ),
		this.options.interval
	);
};

BadgeTextAnimator.prototype.stop = function () {
	clearInterval ( this.intervalId );
	this.intervalId = null;

	chrome.browserAction.setBadgeText ( { text: '' } );
};
// end of badge text animator

</script>
</head>
<body onload="init()">
<img id="imgBlingText" src="bt_small.png" />
<canvas id="canvas" width="19" height="19" />
</body>
</html>